package com.mingqijia.gassafety.console.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.entity.Supplier;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.QuerySupplierRequest;
import com.mingqijia.gassafety.webserver.request.SupplierRequest;
import com.mingqijia.gassafety.webserver.service.SupplierService;
import com.mingqijia.gassafety.webserver.webSocket.WebSocketProcess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName SupplierController.java
 * @Description 供应商
 * @createTime 2022年06月08日
 */

@RestController
@Api(tags = "供应商管理")
@RequestMapping("/business/v1/supplier")
@Slf4j
public class SupplierController {

    @Autowired
    private SupplierService supplierService;
    @Autowired
    private WebSocketProcess webSocketProcess;

    /**
     * 新增供应商
     *
     * @param request
     * @return
     */
    @PostMapping("saveSupplier")
    @ApiOperation(value = "新增供应商")
    public CommonResponse<Object> saveSupplier(@RequestBody @Valid SupplierRequest request) {
        supplierService.saveSupplier(request);
        return CommonResponse.success(null);
    }

    /**
     * 编辑供应商
     *
     * @param request
     * @return
     */
    @PostMapping("editSupplier")
    @ApiOperation(value = "编辑供应商")
    public CommonResponse<Object> editSupplier(@RequestBody @Valid SupplierRequest request) {
        supplierService.editSupplier(request);
        return CommonResponse.success(null);
    }

    /**
     * 删除供应商
     *
     * @param id
     * @return
     */
    @GetMapping("deleteSupplier")
    @ApiOperation(value = "删除供应商")
    public CommonResponse<Object> deleteSupplier(@RequestParam("id") Long id) {
        supplierService.deleteSupplier(id);
        return CommonResponse.success(null);
    }

    @GetMapping("sendMessage")
    @ApiOperation(value = "删除供应商")
    public CommonResponse<Object> sendMessage(@RequestParam("id") Long id,@RequestParam("spId") String spId) {
        try {

            JSONObject object = new JSONObject();
            Map<String, Object> messageMap = new HashMap<>();
            messageMap.put("notifyReason","WarnStrategy");
            messageMap.put("notifyName","设备触发报警策略");
            messageMap.put("notifyTime", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            messageMap.put("imei","6666666666" + "-" + "1-1");
            messageMap.put("installationPosition","上海市浦东新区");
            messageMap.put("equipmentTypeName","测试设备");
            messageMap.put("xlel","90");
            messageMap.put("consumerId","11");
            messageMap.put("custName","这是户名");
            messageMap.put("subsCode","这是户号");
            messageMap.put("isSystem",true);
            messageMap.put("isPop",true);
            object.put("spId",spId);
            object.put("message",messageMap);
            if (id==1){
//                webSocketProcess.sendAllMessage("322323");
            }else if (id==2){
                webSocketProcess.redisTopicSendMsg(spId,JSONObject.toJSONString(messageMap));
            }else if (id==3){
                webSocketProcess.redisTopicSendMsg("spid1","3434");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResponse.success(null);
    }
    /**
     * 查询供应商
     *
     * @return
     */
    @PostMapping("querySupplier")
    @ApiOperation(value = "查询供应商")
    public CommonResponse<PageUtil<Supplier>> querySupplier(@RequestBody @Valid QuerySupplierRequest request) {
        return CommonResponse.success(supplierService.querySupplier(request));
    }

    /**
     * 企业列表
     * @param
     * @return
     */
    @GetMapping("queryEnterpriseList")
    @ApiOperation(value = "企业列表")
    public CommonResponse<Object> queryEnterpriseList() {
        return CommonResponse.success(supplierService.queryEnterprise());
    }

}